﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public interface IReadOnlyDataGridSettings
    {
        bool EnableColumnMenus
        {
            get;
        }

        bool EnableColumnHidingAndShowing
        {
            get;
        }

        int MaximumNumberOfVisibleColumns
        {
            get;
        }

        int MinimumNumberOfVisibleColumns
        {
            get;
        }

        bool RenderHiddenColumns
        {
            get;
        }

        bool EnableColumnReordering
        {
            get;
        }

        bool EnableColumnResizing
        {
            get;
        }

        int DefaultColumnWidth
        {
            get;
        }

        int MinimumColumnWidth
        {
            get;
        }

        int MaximumColumnWidth
        {
            get;
        }

        bool AdjustColumnWidthsOnInitialDisplay
        {
            get;
        }

        bool EnableColumnSorting
        {
            get;
        }

        DataExpression SortByColumnExpression
        {
            get;
        }

        bool IsSortedInDescendingOrder
        {
            get;
        }

        int DefaultRowHeight
        {
            get;
        }

        int? BodyRowHeight
        {
            get;
        }

        int? HeaderRowHeight
        {
            get;
        }

        int? FooterRowHeight
        {
            get;
        }

        int? FilterRowHeight
        {
            get;
        }

        int DefaultOrSetBodyRowHeight
        {
            get;
        }

        int DefaultOrSetHeaderRowHeight
        {
            get;
        }

        int DefaultOrSetFooterRowHeight
        {
            get;
        }

        int DefaultOrSetFilterRowHeight
        {
            get;
        }

        int? ChildRowHeight
        {
            get;
        }

        int DefaultChildRowHeight
        {
            get;
        }

        int DefaultOrSetChildRowHeight
        {
            get;
        }

        int MinimumViewPortHeight
        {
            get;
        }

        int? MaximumViewPortHeight
        {
            get;
        }

        int DefaultMaximumViewPortHeight
        {
            get;
        }

        int DefaultOrSetMaximumViewPortHeight
        {
            get;
        }

        bool EnableViewPortResizing
        {
            get;
        }

        int RecordStartIndex
        {
            get;
        }

        int? RecordSetSize
        {
            get;
        }

        int? TotalRecordCount
        {
            get;
        }

        DataGridPagingType PagingType
        {
            get;
        }

        bool ApplyVirtualizedScrolling
        {
            get;
        }

        bool ApplyStaticRowHeight
        {
            get;
        }

        IList<int> PageSizeOptions
        {
            get;
        }

        string FieldNamePrefix
        {
            get;
        }

        string HttpMethod
        {
            get;
        }

        bool EnableAjaxBinding
        {
            get;
        }

        string SelectUrl
        {
            get;
        }

        DataGridEditMode EditMode
        {
            get;
        }

        string EditUrl
        {
            get;
        }

        string ActiveRowKey
        {
            get;
        }

        DataGridRenderMode RenderMode
        {
            get;
        }

        bool EnablePersistence
        {
            get;
        }

        string PersistenceKey
        {
            get;
        }

        string PersistenceUrl
        {
            get;
        }

        IList<string> IncludeInSelection
        {
            get;
        }

        IList<string> ExcludeFromSelection
        {
            get;
        }

        bool IncludeInSelectionByDefault
        {
            get;
        }
    }

    public sealed class DataGridSettings : IReadOnlyDataGridSettings
    {
        public DataGridSettings()
        {
            MinimumNumberOfVisibleColumns = 1;
            MaximumNumberOfVisibleColumns = 50;
            MinimumColumnWidth = 10;
            MaximumColumnWidth = 1000;
            DefaultColumnWidth = 150;
            DefaultRowHeight = 50;
            DefaultChildRowHeight = 250;
            MinimumViewPortHeight = 100;
            DefaultMaximumViewPortHeight = 300;
            HttpMethod = "get";
        }

        public bool EnableColumnMenus
        {
            get;
            set;
        }

        public bool EnableColumnHidingAndShowing
        {
            get;
            set;
        }

        public int MaximumNumberOfVisibleColumns
        {
            get;
            set;
        }

        public int MinimumNumberOfVisibleColumns
        {
            get;
            set;
        }

        public bool RenderHiddenColumns
        {
            get;
            set;
        }

        public bool EnableColumnReordering
        {
            get;
            set;
        }

        public bool EnableColumnResizing
        {
            get;
            set;
        }

        public int DefaultColumnWidth
        {
            get;
            set;
        }

        public int MinimumColumnWidth
        {
            get;
            set;
        }

        public int MaximumColumnWidth
        {
            get;
            set;
        }

        public bool AdjustColumnWidthsOnInitialDisplay
        {
            get;
            set;
        }

        public bool EnableColumnSorting
        {
            get;
            set;
        }

        public DataExpression SortByColumnExpression
        {
            get;
            set;
        }

        public bool IsSortedInDescendingOrder
        {
            get;
            set;
        }

        public int DefaultRowHeight
        {
            get;
            set;
        }

        public int? BodyRowHeight
        {
            get;
            set;
        }

        public int? HeaderRowHeight
        {
            get;
            set;
        }

        public int? FooterRowHeight
        {
            get;
            set;
        }

        public int? FilterRowHeight
        {
            get;
            set;
        }

        public int DefaultOrSetBodyRowHeight
        {
            get
            {
                if (BodyRowHeight.HasValue)
                {
                    return BodyRowHeight.Value;
                }
                return DefaultRowHeight;
            }
        }

        public int DefaultOrSetHeaderRowHeight
        {
            get
            {
                if (HeaderRowHeight.HasValue)
                {
                    return HeaderRowHeight.Value;
                }
                return DefaultRowHeight;
            }
        }

        public int DefaultOrSetFooterRowHeight
        {
            get
            {
                if (FooterRowHeight.HasValue)
                {
                    return FooterRowHeight.Value;
                }
                return DefaultRowHeight;
            }
        }

        public int DefaultOrSetFilterRowHeight
        {
            get
            {
                if (FilterRowHeight.HasValue)
                {
                    return FilterRowHeight.Value;
                }
                return DefaultRowHeight;
            }
        }

        public int? ChildRowHeight
        {
            get;
            set;
        }

        public int DefaultChildRowHeight
        {
            get;
            set;
        }

        public int DefaultOrSetChildRowHeight
        {
            get
            {
                if (ChildRowHeight.HasValue)
                {
                    return ChildRowHeight.Value;
                }
                return DefaultChildRowHeight;
            }
        }

        public int MinimumViewPortHeight
        {
            get;
            set;
        }

        public int? MaximumViewPortHeight
        {
            get;
            set;
        }

        public int DefaultMaximumViewPortHeight
        {
            get;
            set;
        }

        public int DefaultOrSetMaximumViewPortHeight
        {
            get
            {
                if (MaximumViewPortHeight.HasValue)
                {
                    return MaximumViewPortHeight.Value;
                }
                return DefaultMaximumViewPortHeight;
            }
        }

        public bool EnableViewPortResizing
        {
            get;
            set;
        }

        public int RecordStartIndex
        {
            get;
            set;
        }

        public int? RecordSetSize
        {
            get;
            set;
        }

        public int? TotalRecordCount
        {
            get;
            set;
        }

        public DataGridPagingType PagingType
        {
            get;
            set;
        }

        public bool ApplyVirtualizedScrolling
        {
            get
            {
                return (
                    PagingType == DataGridPagingType.BufferedPaging ||
                    PagingType == DataGridPagingType.BufferedScrolling ||
                    PagingType == DataGridPagingType.VirtualPaging
                );
            }
        }

        public bool ApplyStaticRowHeight
        {
            get
            {
                return (
                    BodyRowHeight.HasValue ||
                    ApplyVirtualizedScrolling
                );
            }
        }

        public IList<int> PageSizeOptions
        {
            get;
            set;
        }

        public string FieldNamePrefix
        {
            get;
            set;
        }

        public string HttpMethod
        {
            get;
            set;
        }

        public bool EnableAjaxBinding
        {
            get;
            set;
        }

        public string SelectUrl
        {
            get;
            set;
        }

        public DataGridEditMode EditMode
        {
            get;
            set;
        }

        public string EditUrl
        {
            get;
            set;
        }

        public string ActiveRowKey
        {
            get;
            set;
        }

        public DataGridRenderMode RenderMode
        {
            get;
            set;
        }

        public bool EnablePersistence
        {
            get;
            set;
        }

        public string PersistenceKey
        {
            get;
            set;
        }

        public string PersistenceUrl
        {
            get;
            set;
        }

        public IList<string> IncludeInSelection
        {
            get;
            set;
        }

        public IList<string> ExcludeFromSelection
        {
            get;
            set;
        }

        public bool IncludeInSelectionByDefault
        {
            get;
            set;
        }

        public bool IsSortByColumn(DataGridColumnSettings column)
        {
            return (
                SortByColumnExpression != null && 
                !string.IsNullOrEmpty(SortByColumnExpression.Value) && 
                SortByColumnExpression.Value == column.SortExpression.Value
            );
        }
    }
}
